using System;
using System.Collections.Generic;
using Alerter.DataSet;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace Alerter.Controls
{
    public partial class MessageContactXtraForm : XtraForm
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set { _controller = value; }
        }

        public List<long> SelectedGroups { set; get; }

        public List<long> SelectedContacts { set; get; }

        #endregion

        public MessageContactXtraForm()
        {
            InitializeComponent();
            Load += MessageContactXtraFormLoad;
        }

        private void SetGroupGridControl()
        {
            var dataSet = _controller.GetGroups();
            groupGridControl.DataSource = null;
            groupGridControl.DataSource = dataSet;
            groupGridControl.DataMember = dataSet.Grupo.TableName;
        }

        private void SetContactGridControl()
        {
            var dataSet = _controller.GetContacts();
            contactGridControl.DataSource = null;
            contactGridControl.DataSource = dataSet;
            contactGridControl.DataMember = dataSet.Contacto.TableName;
        }

        private void SetupOperatorLookupData()
        {
            operadoraDataSet = _controller.GetOperators();
            operatorRepositoryItemLookUpEdit.Columns.Clear();
            operatorRepositoryItemLookUpEdit.Columns.AddRange(new[] {
                new LookUpColumnInfo("Id", "Id", 81, FormatType.None, "", false, HorzAlignment.Near),
                new LookUpColumnInfo("Nome", "Nome", 85, FormatType.None, "", true, HorzAlignment.Near)});
            operatorRepositoryItemLookUpEdit.DisplayMember = operadoraDataSet.Operadora.NomeColumn.ColumnName;
            operatorRepositoryItemLookUpEdit.ValueMember = operadoraDataSet.Operadora.IdColumn.ColumnName;
            operatorRepositoryItemLookUpEdit.DataSource = operadoraDataSet.Operadora;
        }


        void MessageContactXtraFormLoad(object sender, EventArgs e)
        {
            SetGroupGridControl();
            SetContactGridControl();
            SetupOperatorLookupData();
            groupGridView.CustomUnboundColumnData += GroupGridViewCustomUnboundColumnData;
            contactGridView.CustomUnboundColumnData += ContactGridViewCustomUnboundColumnData;
        }
        
        void GroupGridViewCustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            var group = (GrupoDataSet.GrupoRow) groupGridView.GetDataRow(e.RowHandle);
            if (e.IsSetData)
            {
                if (Convert.ToBoolean(e.Value) && !SelectedGroups.Contains(group.Id)) SelectedGroups.Add(group.Id);
                if (!Convert.ToBoolean(e.Value) && SelectedGroups.Contains(group.Id)) SelectedGroups.Remove(group.Id);
            }    
            if (e.IsGetData)
                e.Value = SelectedGroups.Contains(group.Id);            
        }

        void ContactGridViewCustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            var contact = (ContactoDataSet.ContactoRow)contactGridView.GetDataRow(e.RowHandle);
            if (e.IsSetData)
            {
                if (Convert.ToBoolean(e.Value) && !SelectedContacts.Contains(contact.Id)) SelectedContacts.Add(contact.Id);
                if (!Convert.ToBoolean(e.Value) && SelectedContacts.Contains(contact.Id)) SelectedContacts.Remove(contact.Id);
            }    
            if (e.IsGetData)
                e.Value = SelectedContacts.Contains(contact.Id);            
        }

    }
}